/*	$Id: private.h 1116 2010-12-12 20:56:04Z phrakt $	*/
/*
 * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _CANOPY_PRIVATE_H_
#define _CANOPY_PRIVATE_H_

#include <sys/types.h>

#include <canopy/cdefs.h>
#include <canopy/queue.h>

#include <pthread.h>

#include "log.h"
#include "error.h"
#include "thread.h"



struct cnp_queue {
	void           **q_data;
	unsigned int     q_capacity;
	unsigned int     q_count;
	unsigned int     q_head;
	unsigned int     q_tail;
	cnp_mutex_t      q_lock;
	cnp_cond_t       q_cond;
};



struct cnp_thread {
	pthread_t                     thr_hdl;
	char                          thr_name[CNP_THR_MAXNAMELEN];
	void                       *(*thr_main)(void *);
	void                         *thr_arg;
	volatile enum cnp_thr_state   thr_state;
	cnp_rwlock_t                  thr_lock;
	cnp_thrpool_t                *thr_pool;

	TAILQ_ENTRY(cnp_thread)        thr_link;
};



struct cnp_thread_pool {

	/* pool name */
	char                     tp_name[CNP_THRPOOL_MAXNAMELEN];

	int                      tp_flags;

	/* minimum, maximum and current thread count */
	int16_t                  tp_thrmin;
	int16_t                  tp_thrmax;
	int16_t                  tp_thrcount;

	sigset_t                 tp_sigset;
	pthread_attr_t           tp_thrattr;

	/* main routine for threads in this pool */
	void                  *(*tp_main)(void *);

	TAILQ_HEAD(, cnp_thread) tp_threads;

	pthread_mutex_t          tp_lock;
};



extern cnp_log_t  *cnp_logchan;



int   cnp_init          (void) ATTR_CONSTRUCTOR;
void  cnp_cleanup       (void) ATTR_DESTRUCTOR;

int   cnp_error_init    (void);
void  cnp_error_cleanup (void);

int   cnp_mem_init      (void);
void  cnp_mem_cleanup   (void);

int   cnp_thr_init      (void);
void  cnp_thr_cleanup   (void);

#endif /* _CANOPY_PRIVATE_H_ */
